package com.example.hszpad.fragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.hszpad.R;
import com.example.hszpad.adapter.BaseRecyclerAdapter;
import com.example.hszpad.adapter.SmartViewHolder;
import com.example.hszpad.bean.MenuItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HSZViewFragmentPower extends Fragment {
    private static final String TAG = "HSZViewFragmentPower";
    private ImageView iv_power;
    private RecyclerView recyclerView;
    private BaseRecyclerAdapter<MenuItemBean> recyclerChildAdapter;
    private List<MenuItemBean> menuItemBeanChildList;
    private MenuItemBean curChildBean;

    private AbsoluteLayout fl_content;
    private int changeState = 1; //1常规参数 2智能化参数

    public HSZViewFragmentPower() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_hszview_power, container, false);
        iv_power = view.findViewById(R.id.iv_power);
        recyclerView = view.findViewById(R.id.recyclerView);
        fl_content = view.findViewById(R.id.fl_content);
        view.findViewById(R.id.btn_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (curChildBean != null) {
                    if (curChildBean.getId() == 1) {
                        if (changeState == 1) {
                            Glide.with(getActivity()).load(R.drawable.ic_content_power2).into(iv_power);
                            changeState = 2;
                        } else {
                            Glide.with(getActivity()).load(R.drawable.ic_content_power1).into(iv_power);
                            changeState = 1;
                        }
                    } else {
                        if (changeState == 1) {
                            Glide.with(getActivity()).load(R.drawable.ic_content_power4).into(iv_power);
                            changeState = 2;
                        } else {
                            Glide.with(getActivity()).load(R.drawable.ic_content_power3).into(iv_power);
                            changeState = 1;
                        }
                    }
                }
            }
        });
        fl_content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                fl_content.getViewTreeObserver().removeOnPreDrawListener(this);
                int width = fl_content.getWidth();
                int height = fl_content.getHeight();
                Log.e(TAG, "width:" + width + "-----------height:" + height);
                return true;
            }
        });

        AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(
                1632,
                1062,
                0,
                0);
        iv_power.setLayoutParams(layoutParams);
//        al_main.addView(img);
//        bean.setImageView(img);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        menuItemBeanChildList = new ArrayList<>();
        MenuItemBean bean = new MenuItemBean(1, "柴油机系统", 1, R.drawable.ic_home_white_24dp, true);
        menuItemBeanChildList.add(bean);
        bean = new MenuItemBean(2, "轴系/齿轮箱", 1, R.drawable.ic_home_white_24dp, false);
        menuItemBeanChildList.add(bean);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerChildAdapter = new BaseRecyclerAdapter<MenuItemBean>(menuItemBeanChildList, R.layout.layout_view_menu_item, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MenuItemBean itemBean = menuItemBeanChildList.get(position);
                if (itemBean.isSelect()) {
                    return;
                } else {
                    for (int i = 0; i < menuItemBeanChildList.size(); i++) {
                        menuItemBeanChildList.get(i).setSelect(false);
                    }
                    itemBean.setSelect(true);
                    if (itemBean != curChildBean) {
                        changeState = 1;
                        if (itemBean.getId() == 1) {
                            Glide.with(getActivity()).load(R.drawable.ic_content_power1).into(iv_power);
                        } else {
                            Glide.with(getActivity()).load(R.drawable.ic_content_power3).into(iv_power);
                        }
                        curChildBean = itemBean;
                    }
                }
                recyclerChildAdapter.notifyDataSetChanged();
                recyclerChildAdapter.notifyListDataSetChanged();
            }
        }) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, MenuItemBean model, int position) {
                holder.text(R.id.tv_title, model.getName());
                if (model.isSelect()) {
                    holder.viewBgImageId(R.id.ll_content, R.drawable.ic_bg_menu_pressed);
                    holder.textColorId(R.id.tv_title, R.color.colorSecondMenuNormal);
                } else {
                    holder.viewBgImageId(R.id.ll_content, R.drawable.ic_bg_menu_normal);
                    holder.textColorId(R.id.tv_title, R.color.colorSecondMenuPressed);
                }
            }
        };
        recyclerView.setAdapter(recyclerChildAdapter);
        curChildBean = menuItemBeanChildList.get(0);
        changeState = 1;
        Glide.with(getActivity()).load(R.drawable.ic_content_power1).into(iv_power);
        return view;
    }

}
